package com.rd.student.ruanjian.class4.wangxingkai.TestOn_7_11;

import java.util.ArrayList;
import java.util.Collections;

public class Test3 {
    public static void main(String[] args) {


        ArrayList arrayList = new ArrayList();
        arrayList.add(new D(17));
        arrayList.add(new D(20));
        arrayList.add(new D(15));
        arrayList.add(new D(18));

        Collections.sort(arrayList);
        System.out.println(arrayList);
        // 15 17 18 20
        // 20 18 17 15
    }
    static class D implements Comparable {
        int age;

        public D(int age) {
            this.age = age;
        }

        // 20.compareTo(10)
        // return 1  调换顺序
        //  两个对象相等  返回0
        //  return -1 默认不调换
        //  10 20
        @Override
        public int compareTo(Object o) {
            D d = (D) o;
            if (this.age > d.age) {
//            return 1;
                return 1;
            } else if (this.age < d.age) {
//            return -1;
                return -1;
            }
            return 0;
        }

        @Override
        public String toString() {
            return "D{" +
                    "age=" + age +
                    '}';
        }
    }




}
